<div id="tb<?php echo $uniqid;?>" style="padding:5px">
	<table cellspacing="0" cellpadding="0">
		<tbody>
		<tr>
			<td><a href="javascript:void(0);" onclick="agentReload();" class="l-btn l-btn-small l-btn-plain"><span
					class="l-btn-left l-btn-icon-left"><i class="iconfont icon-shuaxin"></i><span class="l-btn-text">重载</span></span></a></td>
			<td><span style="line-height:30px;">注册日期</span>
				<input id="agentStartOn" style="width:110px" type="text" class="easyui-datebox"> </input>至
				<input id="agentEndOn" style="width:110px" type="text" class="easyui-datebox"> </input>
			</td>
			<td>
				&nbsp;&nbsp;
			</td>
			<td>
				<select v-model="level" @change="changeLevel" class="forminput inputstyle textbox-text validatebox-text" style="width:100px">
					<option value="">全部身份</option>
					<option v-for="(vo,index) in agentLevels" :value="vo.level">{{ vo.name }}</option>
				</select></td>
			<td>
				<div class="datagrid-btn-separator"></div>
			</td>
			<td>
				<select id="agentKeywordsType" class="forminput inputstyle textbox-text validatebox-text" style="width:80px">
					<option value="id">会员ID</option>
					<option value="nickname">会员昵称</option>
					<option value="phone">手机号</option>
				</select></td>
			<td>&nbsp&nbsp;<input id="agentKeywords" style="width:100px" type="text" class="easyui-input textbox"></td>
			<td><a href="javascript:void(0);" onclick="agentSearch();" class="l-btn l-btn-small l-btn-plain"><span
					class="l-btn-left l-btn-icon-left"><i class="iconfont icon-sousuo"></i><span class="l-btn-text">查询</span></span></a></td>
			<td><a href="javascript:void(0);" onclick="agentExport();" class="l-btn l-btn-small l-btn-plain"><span
					class="l-btn-left l-btn-icon-left"><i class="iconfont icon-xiazai"></i><span
					class="l-btn-text">导出代理商</span></span></a></td>
		</tr>
		</tbody>
	</table>
</div>
<div class="con"  style="-moz-user-select:none;">
	<table id="agent"></table>
</div>
<div id="agentDlg" class="easyui-dialog" style="width:840px; height:520px;" title="信息框" closed="true"></div>
<script>
	var memberTeams = function (nickname, id) {
		let name = nickname || ($('#agent').datagrid('getSelected') && $('#agent').datagrid('getSelected').nickname);
		let rowId = id || ($('#agent').datagrid('getSelected') && $('#agent').datagrid('getSelected').id);
		if (rowId) {
			$("#agentDlg").dialog({
				title: name + '-团队信息',
				closed: false,
				width: 940,
				height: 560,
				cache: false,
				href: '/admin/agent/team?id=' + rowId + '&name=' + name,
				modal: true,
				buttons: [{
					text: '关闭',
					iconCls: 'icon-no',
					handler: function () {
						$("#agentDlg").dialog('close');
					}
				}]
			});
		} else {
			$.messager.alert('提示', '请先选择要查看的记录！', 'warning');
		}
	};
	var memberTeamsV1 = function (nickname, id) {
		let name = nickname || ($('#agent').datagrid('getSelected') && $('#agent').datagrid('getSelected').nickname);
		let rowId = id || ($('#agent').datagrid('getSelected') && $('#agent').datagrid('getSelected').id);
		if (rowId) {
			$("#agentDlg").dialog({
				title: name + '-直推团队信息',
				closed: false,
				width: 940,
				height: 560,
				cache: false,
				href: '/admin/member/teamv1?id=' + rowId + '&name=' + name,
				modal: true,
				buttons: [{
					text: '关闭',
					iconCls: 'icon-no',
					handler: function () {
						$("#agentDlg").dialog('close');
					}
				}]
			});
		} else {
			$.messager.alert('提示', '请先选择要查看的记录！', 'warning');
		}
	};
	var teamConsume = function (nickname, id) {
		let name = nickname || ($('#agent').datagrid('getSelected') && $('#agent').datagrid('getSelected').nickname);
		let rowId = id || ($('#agent').datagrid('getSelected') && $('#agent').datagrid('getSelected').id);
		if (rowId) {
			$("#agentDlg").dialog({
				title: name + '-团队业绩明细',
				closed: false,
				width: 940,
				height: 560,
				cache: false,
				href: '/admin/member/teamConsume?id=' + rowId + '&name=' + name,
				modal: true,
				buttons: [{
					text: '关闭',
					iconCls: 'icon-no',
					handler: function () {
						$("#agentDlg").dialog('close');
					}
				}]
			});
		} else {
			$.messager.alert('提示', '请先选择要查看的记录！', 'warning');
		}
	};
	var cfteamConsume = function (nickname, id) {
		let name = nickname || ($('#agent').datagrid('getSelected') && $('#agent').datagrid('getSelected').nickname);
		let rowId = id || ($('#agent').datagrid('getSelected') && $('#agent').datagrid('getSelected').id);
		if (rowId) {
			$("#agentDlg").dialog({
				title: name + '-财富区业绩明细',
				closed: false,
				width: 940,
				height: 560,
				cache: false,
				href: '/admin/member/cfteamConsume?id=' + rowId + '&name=' + name,
				modal: true,
				buttons: [{
					text: '关闭',
					iconCls: 'icon-no',
					handler: function () {
						$("#agentDlg").dialog('close');
					}
				}]
			});
		} else {
			$.messager.alert('提示', '请先选择要查看的记录！', 'warning');
		}
	};
	var teamConsumeV1 = function (nickname, id) {
		let name = nickname || ($('#agent').datagrid('getSelected') && $('#agent').datagrid('getSelected').nickname);
		let rowId = id || ($('#agent').datagrid('getSelected') && $('#agent').datagrid('getSelected').id);
		if (rowId) {
			$("#agentDlg").dialog({
				title: name + '-直推业绩明细',
				closed: false,
				width: 940,
				height: 560,
				cache: false,
				href: '/admin/member/teamConsumeV1?id=' + rowId + '&name=' + name,
				modal: true,
				buttons: [{
					text: '关闭',
					iconCls: 'icon-no',
					handler: function () {
						$("#agentDlg").dialog('close');
					}
				}]
			});
		} else {
			$.messager.alert('提示', '请先选择要查看的记录！', 'warning');
		}
	};
	var totalConsume = function (nickname, id) {
		let name = nickname || ($('#agent').datagrid('getSelected') && $('#agent').datagrid('getSelected').nickname);
		let rowId = id || ($('#agent').datagrid('getSelected') && $('#agent').datagrid('getSelected').id);
		if (rowId) {
			$("#agentDlg").dialog({
				title: name + '-个人消费明细',
				closed: false,
				width: 940,
				height: 560,
				cache: false,
				href: '/admin/member/totalConsume?id=' + rowId + '&name=' + name,
				modal: true,
				buttons: [{
					text: '关闭',
					iconCls: 'icon-no',
					handler: function () {
						$("#agentDlg").dialog('close');
					}
				}]
			});
		} else {
			$.messager.alert('提示', '请先选择要查看的记录！', 'warning');
		}
	};
	var totalIncome = function (nickname, id) {
		let rowId = id || ($('#agent').datagrid('getSelected') && $('#agent').datagrid('getSelected').id);
		if (rowId) {
			let url	  =	'/admin/commissionlogs/index?id=' + rowId;
			let title = '佣金明细';
			$('#rightTabs').tabs('close',title);
			addTab(title,url);
		} else {
			$.messager.alert('提示', '请先选择要查看的记录！', 'warning');
		}
	};
	var agentVm = new Vue({
		el: '#tb<?php echo $uniqid;?>',
		data: {
			level: '',
			agentLevels: JSON.parse('<?php echo addslashes($agentLevel);?>'),
		},
		methods: {
			changeLevel(){
				agentSearch();
			}
		}
	});

	var agentEdit = function (id) {
		let rowId = id || ($('#agent').datagrid('getSelected') && $('#agent').datagrid('getSelected').id);
		if (rowId) {
			$("#agentDlg").dialog({
				title: '编辑信息',
				closed: false,
				width: 840,
				height: 520,
				cache: false,
				href: '/admin/agent/edit?id=' + rowId,
				modal: true,
				buttons: [{
					text: '保存',
					id: 'agentsubmit',
					iconCls: 'icon-ok',
				}, {
					text: '关闭',
					iconCls: 'icon-no',
					handler: function () {
						$("#agentDlg").dialog('close');
					}
				}]
			});
		} else {
			$.messager.alert('提示', '请先选择要编辑的行！', 'warning');
		}
	};
	var agentDelete = function (id) {
		let rowId = id || ($('#agent').datagrid('getSelected') && $('#agent').datagrid('getSelected').id);
		if (rowId) {
			$.messager.confirm('提示', '确定删除吗？', function (r) {
				if (r) {
					$.messager.progress();
					$.post('/admin/agent/delete', {id: row.id}, function (data) {
						$.messager.progress('close');
						if (parseInt(data.ret)===0) {
							$.messager.show({
								title: '提示',
								msg: data.msg,
								timeout: 3000,
								showType: 'slide'
							});
							agentReload();
						} else {
							$.messager.alert('提示', data.msg, 'warning');
						}
					}, 'json');
				}
			});
		} else {
			$.messager.alert('提示', '请先选择要删除的行！', 'warning');
		}
	};
	var agentExport = function () {
		showLoading('.con');
		$.post('/admin/agent/export', {
			start_on: $('#agentStartOn').val(),
			end_on: $('#agentEndOn').val(),
			level: agentVm.level,
			keywordsType: $('#agentKeywordsType').val(),
			keywords: $('#agentKeywords').val(),
		}, function (data) {
			hideLoading();
			if (parseInt(data.ret) === 0) {
				$.messager.show({
					title: '提示',
					msg: data.msg,
					timeout: 3000,
					showType: 'slide'
				});
				eventmaterialDownload(data.data);
			} else {
				$.messager.alert('提示', data.msg, 'warning');
			}
		}, 'json').fail((response)=> {
			hideLoading();
			$.messager.alert('提示', response.responseText, 'warning');
		});
	};
	var agentReload = function () {
		$("#agent").datagrid('reload');
	};
	var agentSearch = function () {
		var height = $(window).height() - $(".top").height() - 46;
		var width = $(window).width() - $(".leftmenu").width() - 8;
		$("#agent").datagrid({
			title: '代理商列表',
			width: width,
			height: height,//高度
			singleSelect: true,//如果为true，则只允许选择一行
			striped: true,//是否显示斑马线效果。
			rownumbers: false,//如果为true，则显示一个行号列。
			pagination: true,//是否显示分页
			pageSize: 10,
			pageList: [10, 15, 20, 25],
			method: 'post',
			sortName: 'id',
			sortOrder: 'desc',
			idField: 'id',
			url: '/admin/agent/index',
			queryParams: {
				start_on: $('#agentStartOn').val(),
				end_on: $('#agentEndOn').val(),
				level: agentVm.level,
				keywordsType: $('#agentKeywordsType').val(),
				keywords: $('#agentKeywords').val(),
			},
			fitColumns: true,//真正的自动展开/收缩列的大小，以适应网格的宽度，防止水平滚动。
			nowrap: true,//如果为true，则在同一行中显示数据。设置为true可以提高加载性能。 是否换行
			selectOnCheck: false,
			checkOnSelect: true,
			toolbar: '#tb<?php echo $uniqid;?>',
			columns: [[
				{field:'id',title:'ID',width:'5%'},
				{field:'avatar',title:'头像',width:'5%',formatter:function (value,rowData,rowIndex){
						return value ? "<img height='50px' src='"+value+"'>" : '';
					}},
				{field:'nickname',title:'用户',width:'10%'},
				{field:'parent_member_id',title:'推荐人',width:'10%'},
				{field:'phone',title:'手机号',width:'10%'},
				{field:'level_info',title:'身份',width:'10%', formatter(value, rowData){
					return value ? value.name : '普通会员';
					}},
				{field:'money',title:'余额',width:'8%',sortable:true},
				{field:'total_income',title:'累计佣金',width:'8%',sortable:true, formatter: function (value, rowData, rowIndex) {
						return "<a style='cursor: pointer' href='javascript:void(0)' onclick='totalIncome(\"" + rowData.nickname + '\",' + rowData.id + ")'>" + value + "</a>";
					}},
				{field:'total_consume',title:'消费金额',width:'8%',sortable:true, formatter: function (value, rowData, rowIndex) {
						return "<a style='cursor: pointer' href='javascript:void(0)' onclick='totalConsume(\"" + rowData.nickname + '\",' + rowData.id + ")'>" + value + "</a>";
					}},
				{
					field: 'child_user_count',
					title: '团队人数',
					width: '8%',
					formatter: function (value, rowData, rowIndex) {
						return value > 0 ? "<a style='cursor: pointer' href='javascript:void(0)' onclick='memberTeams(\"" + rowData.nickname + '\",' + rowData.id + ")'>" + value + "</a>" : 0;
					}
				},
				{field: 'team_consume', title: '团队业绩', width: '8%', sortable: true, formatter: function (value, rowData, rowIndex) {
						return "<a style='cursor: pointer' href='javascript:void(0)' onclick='teamConsume(\"" + rowData.nickname + '\",' + rowData.id + ")'>" + value + '/' + rowData.team_consume_num + "单</a>";
					}},
				{field: 'cfteam_consume', title: '财富区业绩', width: '8%', sortable: true, formatter: function (value, rowData, rowIndex) {
						return "<a style='cursor: pointer' href='javascript:void(0)' onclick='cfteamConsume(\"" + rowData.nickname + '\",' + rowData.id + ")'>" + value + '/' + rowData.cfteam_consume_num + "单</a>";
					}},
				{field:'child_user_count_v1',title:'直推人数',width:'8%',sortable:true,
					formatter: function (value, rowData, rowIndex) {
						return value > 0 ? "<a style='cursor: pointer' href='javascript:void(0)' onclick='memberTeamsV1(\"" + rowData.nickname + '\",' + rowData.id + ")'>" + value + "</a>" : 0;
					}},
				{field:'direct_consume',title:'直推业绩',width:'8%',sortable:true, formatter: function (value, rowData, rowIndex) {
						return "<a style='cursor: pointer' href='javascript:void(0)' onclick='teamConsumeV1(\"" + rowData.nickname + '\",' + rowData.id + ")'>" + value + '/' + rowData.direct_consume_num + "单</a>";
					}},
				{field:'status',title:'状态',width:'8%',formatter:function (value,rowData,rowIndex){
						return value ? '显示' : '隐藏';
					}},
				{field:'created_at',title:'加入时间',width:'10%',sortable:true}
			]],
		});
	};
	onDatagridResize("#agent");
	agentSearch();
</script>
